﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZG_member.BLL;
using System.Configuration;

namespace ZG_member
{
    public partial class OperatorInfo : Form
    {
        public OperatorInfo()
        {
            InitializeComponent();
            this.Opacity = 0;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
        }


        private void OperatorInfo_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
            dgvoperatorInfoBind();
        }
        public void dgvoperatorInfoBind()
        {
            operatorInfo info = new operatorInfo();
            DataTable dt = info.GetList("").Tables[0];
            dgvoperatorInfo.DataSource = dt;
            dgvoperatorInfo.AllowUserToAddRows = false;
            dgvoperatorInfo.Columns["password"].Visible = false;
            foreach (DataGridViewRow dgvr in dgvoperatorInfo.Rows)
            {
                if (dgvr.Cells["sex"].Value != null)
                {
                    if (dgvr.Cells["sex"].Value + "" == "False")
                    {
                        dgvr.Cells["sexNew"].Value = "女";
                    }
                    else
                    {
                        dgvr.Cells["sexNew"].Value = "男";
                    }
                }
                if (dgvr.Cells["roleid"].Value != null)
                {
                    roleInfo role = new roleInfo();
                    Model.roleInfo roleModel = new Model.roleInfo();
                    roleModel = role.GetModelList("id='" + int.Parse(dgvr.Cells["roleid"].Value.ToString()) + "'")[0];
                    dgvr.Cells["roleIdNew"].Value = roleModel.roleName;
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ID = 0;
            UserAdd user = new UserAdd(this);
            user.ShowDialog();
        }
        public int ID;
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            ID = int.Parse(dgvoperatorInfo.CurrentRow.Cells["id"].Value.ToString());
            UserAdd user = new UserAdd(this);
            if (dgvoperatorInfo.CurrentRow.Cells["username"].Value != null)
            {
                user.txtusername.Text = dgvoperatorInfo.CurrentRow.Cells["username"].Value.ToString();
            }
            if (dgvoperatorInfo.CurrentRow.Cells["name"].Value != null)
            {
                user.txtname.Text = dgvoperatorInfo.CurrentRow.Cells["name"].Value.ToString();
            }
            if (dgvoperatorInfo.CurrentRow.Cells["sexNew"].Value != null)
            {
                user.cbxsex.Text = dgvoperatorInfo.CurrentRow.Cells["sexNew"].Value.ToString();
            }
            if (dgvoperatorInfo.CurrentRow.Cells["phone"].Value != null)
            {
               user.txtphone.Text = dgvoperatorInfo.CurrentRow.Cells["phone"].Value.ToString();
            }
            if (dgvoperatorInfo.CurrentRow.Cells["roleIdNew"].Value != null)
            {
                user.cbxroleId.Text = dgvoperatorInfo.CurrentRow.Cells["roleIdNew"].Value.ToString();
            }
            user.ShowDialog();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvoperatorInfo.CurrentRow.Cells["id"].Value != null)
            {
                int ID = int.Parse(dgvoperatorInfo.CurrentRow.Cells["id"].Value.ToString());
                operatorInfo info = new operatorInfo();
                DialogResult dt = MessageBox.Show("您确定要删除该条信息吗？", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (dt == DialogResult.Yes)
                {
                    if (info.Delete(ID))
                    {
                        dgvoperatorInfoBind();
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.timer2.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }

        }
        int flag = 0;
        private void OperatorInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void dgvoperatorInfo_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            ID = int.Parse(dgvoperatorInfo.CurrentRow.Cells["id"].Value.ToString());
            UserAdd user = new UserAdd(this);
            if (dgvoperatorInfo.CurrentRow.Cells["username"].Value != null)
            {
                user.txtusername.Text = dgvoperatorInfo.CurrentRow.Cells["username"].Value.ToString();
            }
            if (dgvoperatorInfo.CurrentRow.Cells["name"].Value != null)
            {
                user.txtname.Text = dgvoperatorInfo.CurrentRow.Cells["name"].Value.ToString();
            }
            if (dgvoperatorInfo.CurrentRow.Cells["sexNew"].Value != null)
            {
                user.cbxsex.Text = dgvoperatorInfo.CurrentRow.Cells["sexNew"].Value.ToString();
            }
            if (dgvoperatorInfo.CurrentRow.Cells["phone"].Value != null)
            {
                user.txtphone.Text = dgvoperatorInfo.CurrentRow.Cells["phone"].Value.ToString();
            }
            if (dgvoperatorInfo.CurrentRow.Cells["roleIdNew"].Value != null)
            {
                user.cbxroleId.Text = dgvoperatorInfo.CurrentRow.Cells["roleIdNew"].Value.ToString();
            }
            user.ShowDialog();
        }

        private void dgvoperatorInfo_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
          
            foreach (DataGridViewRow dgvr in dgvoperatorInfo.Rows)
            {
                if (dgvr.Cells["sex"].Value != null)
                {
                    if (dgvr.Cells["sex"].Value + "" == "False")
                    {
                        dgvr.Cells["sexNew"].Value = "女";
                    }
                    else
                    {
                        dgvr.Cells["sexNew"].Value = "男";
                    }
                    if (dgvr.Cells["roleid"].Value != null)
                    {
                        roleInfo role = new roleInfo();
                        Model.roleInfo roleModel = new Model.roleInfo();
                        roleModel = role.GetModelList("id='" + int.Parse(dgvr.Cells["roleid"].Value.ToString()) + "'")[0];
                        dgvr.Cells["roleIdNew"].Value = roleModel.roleName;
                    }
                }

       
            }
        }





       
    }
}
